from django.contrib.auth.models import User
from django.db import models
from datetime import datetime

# ---------------------------
# Definicao das tabelas do BD
# ---------------------------

BRONZE_VALUE   = 15
SILVER_VALUE   = 30
GOLD_VALUE     = 90
PLATINUM_VALUE = 180

class userInfo(models.Model):
	pic_url= models.URLField(default=0, blank=True, verbose_name="URL do Avatar")
	auth   = models.ForeignKey(User, unique=True)
	birth  = models.DateTimeField(default=0, blank=True, verbose_name="Data de Nascimento")
	country= models.IntegerField(default=0, blank=True)

	CHOICES = (
		(0, 'Desativado'),
		(1, 'Ativado'),
	)

	ps3card = models.IntegerField(default=0, choices=CHOICES)
	ps3card_url = models.URLField(default=0, blank=True, verbose_name="URL do Card")

	card = models.IntegerField('myPS3t Card', default=0, choices=CHOICES)
	card_last_trophy = models.IntegerField('Inclusao dos ultimos trofeus', default=1, choices=CHOICES)

	CHOICES = (
		(0, 'Imagem do jogo do ultimo Trofeu'),
		(1, 'Imagem do jogo da ultima Platina'),
		#(2, 'Imagem externa'),
	)

	card_bg_type = models.IntegerField('Fundo do myPS3t Card', default=0, choices=CHOICES)
	card_bg_url = models.URLField('URL da imagem externa', default=0, blank=True)


	def __unicode__(self):
		try:
			return self.auth.username
		except:
			return "-- ERROR -- user_id: %d" %self.id
	
# A ultima vez que o sv atualizou a base inteira
class userLastTrophy(models.Model):
	user     = models.ForeignKey(userInfo)
	platinum = models.IntegerField()
	gold    = models.IntegerField()
	silver  = models.IntegerField()
	bronze  = models.IntegerField()
	level   = models.IntegerField()
	rank    = models.IntegerField()
	perc_level = models.IntegerField()
	date_update = models.DateTimeField(default=datetime.now, blank=True)

	total   = models.IntegerField()
	points  = models.IntegerField()

	def save(self, *args, **kwargs):
		# Compute the total and points before saving
		self.total = int(self.platinum) + int(self.gold) + int(self.silver) + int(self.bronze)
		self.points = self.platinum * PLATINUM_VALUE + self.gold * GOLD_VALUE + \
			self.silver * SILVER_VALUE + self.bronze * BRONZE_VALUE

		# Now save the object by calling the super class
		super(userLastTrophy, self).save(*args, **kwargs)

class userTrophy(models.Model):
	user     = models.ForeignKey(userInfo)
	platinum = models.IntegerField()
	gold    = models.IntegerField()
	silver  = models.IntegerField()
	bronze  = models.IntegerField()
	total   = models.IntegerField()
	level   = models.IntegerField()
	perc_level  = models.IntegerField()
	date_update = models.DateTimeField(default=datetime.now, blank=True)

	rank    = models.IntegerField(default=0)
	total   = models.IntegerField(default=0)
	points  = models.IntegerField(default=0)

	last_trophy = models.ForeignKey(userLastTrophy, default=0)

	def save(self, *args, **kwargs):
		# Compute the total and points before saving
		self.total = int(self.platinum) + int(self.gold) + int(self.silver) + int(self.bronze)
		self.points = self.platinum * PLATINUM_VALUE + self.gold * GOLD_VALUE + \
			self.silver * SILVER_VALUE + self.bronze * BRONZE_VALUE

		# Now save the object by calling the super class
		super(userTrophy, self).save(*args, **kwargs)

class gameInfo(models.Model):
	psn_id = models.CharField(max_length=100)
	name   = models.CharField(max_length=200)
	publisher = models.CharField(max_length=200, default=0)
	developer = models.CharField(max_length=200, default=0)
	release_date = models.DateTimeField(blank=True, null=True)

	date_added = models.DateTimeField(default=datetime.now)
	type_fill = models.IntegerField(default=0)
	# 0 - psn_games
	# 1 - por um usuario sem platina
	# 2 - por um usuario com platina (melhor caso)

	gold    = models.IntegerField(default=0)
	silver  = models.IntegerField(default=0)
	bronze  = models.IntegerField(default=0)
	hidden  = models.IntegerField(default=0)
	total   = models.IntegerField(default=0)

	def __unicode__(self):
		return self.name[:20]

class gameTrophyInfo(models.Model):
	game = models.ForeignKey(gameInfo)
	name = models.CharField(max_length=500)
	desc = models.CharField(max_length=500)
	type = models.CharField(max_length=20)

	pic_url = models.URLField(default=0, blank=True)

	desc_pt = models.CharField(max_length=500, default=0)
	name_pt = models.CharField(max_length=500, default=0)

	hidden_id = models.IntegerField(default=0)
	total_id  = models.IntegerField()
	trophy_id = models.IntegerField()

	addon = models.CharField(default=0, max_length=200)

	def __unicode__(self):
		return self.name[:20]

class gameUserTrophyInfo(models.Model):
	user    = models.ForeignKey(userInfo)
	trophy  = models.ForeignKey(gameTrophyInfo)
	date    = models.DateTimeField()
	date_read = models.CharField(max_length=100, default=0)

class userLastGameInfo(models.Model):
	user      = models.ForeignKey(userInfo)
	game      = models.ForeignKey(gameInfo)
	perc_done = models.IntegerField()
	platinum  = models.IntegerField()
	gold    = models.IntegerField()
	silver  = models.IntegerField()
	bronze  = models.IntegerField()
	total   = models.IntegerField()
	date_update = models.DateTimeField(default=datetime.now, blank=True)

	def save(self, *args, **kwargs):

		self.total = int(self.platinum) + int(self.gold) + int(self.silver) + int(self.bronze)

		# Now save the object by calling the super class
		super(userLastGameInfo, self).save(*args, **kwargs)

class userGameInfo(models.Model):
	user      = models.ForeignKey(userInfo)
	game      = models.ForeignKey(gameInfo)
	perc_done = models.IntegerField()
	platinum  = models.IntegerField()
	gold    = models.IntegerField()
	silver  = models.IntegerField()
	bronze  = models.IntegerField()

	total = models.IntegerField(default=0)
	date_update = models.DateTimeField(default=datetime.now, blank=True)

	last = models.ForeignKey(userLastGameInfo, default=0)

	def save(self, *args, **kwargs):

		self.total = int(self.platinum) + int(self.gold) + int(self.silver) + int(self.bronze)

		# Now save the object by calling the super class
		super(userGameInfo, self).save(*args, **kwargs)

class updateQueue(models.Model):
	user = models.ForeignKey(User)
	type = models.IntegerField()
	date = models.DateTimeField(default=datetime.now, blank=True)

class boost(models.Model):
	creator = models.ForeignKey(userInfo)
	game  = models.ForeignKey(gameInfo)
	name  = models.CharField(max_length=200)
	desc  = models.CharField(max_length=500)
	short_desc  = models.CharField(max_length=200)
	rules = models.CharField(max_length=500)

	CHOICES = (
		(0, 'Nao'),
		(1, 'Sim'),
	)

	subscribe = models.IntegerField(default=0, choices=CHOICES)

	date = models.DateTimeField(default=datetime.now)

	def __unicode__(self):
		return self.name[:20]

class boost_trophy(models.Model):
	boost  = models.ForeignKey(boost)
	trophy = models.ForeignKey(gameTrophyInfo)
	# 0 - Obitiveis
	# 1 - Requisitos minimos

	CHOICES = (
		(1, 'Conquistaveis'),
		(0, 'Requisito Minimo'),
	)

	type   = models.IntegerField(default=0, choices=CHOICES)

	def __unicode__(self):
		return self.trophy.name[:20]

class boost_user(models.Model):
	boost = models.ForeignKey(boost)
	user  = models.ForeignKey(userInfo)
	date  = models.DateTimeField(default=datetime.now)

	def __unicode__(self):
		return self.user.auth.username[:20]

import django_pipes as pipes

# Create your models here.
class GoogleSearch(pipes.Pipe):
	uri = "http://ajax.googleapis.com/ajax/services/search/web"

	@staticmethod
	def fetch(q):
		resp = GoogleSearch.objects.get({'v':1.0, 'q':q})
		if resp and hasattr(resp, "responseData") and hasattr(resp.responseData, "results"):
			return resp.responseData.results

class YoutubeSearch(pipes.Pipe):
	uri = "http://ajax.googleapis.com/ajax/services/search/web"

	@staticmethod
	def fetch(q):
		resp = GoogleSearch.objects.get({'v':1.0, 'q':q})
		if resp and hasattr(resp, "responseData") and hasattr(resp.responseData, "results"):
			return resp.responseData.results
